<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="imlgw,半岛铁盒,blog,Java博客,程序员,个人博客,java開發,程序員,個人博客,Java">
    <meta name="description" content="大悲无泪，大悟无言，大笑无声。">
    <meta name="author" content="Resolmi">
    
    <title>
        
            Redis集群搭建 |
        
        Tadow
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="https://static.imlgw.top/blog/20210731/BtJz541CcmJU.ico">
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/css/font-awesome.min.css">
    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"imlgw.top","root":"/","language":"zh-CN","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":true,"expand_all":true,"init_open":true},"style":{"primary_color":"#0066CC","avatar":"https://static.imlgw.top/blog/20210731/3C7hCSRR3lfq.png","favicon":"https://static.imlgw.top/blog/20210731/BtJz541CcmJU.ico","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":true},"first_screen":{"enable":true,"background_img":"/images/image.svg","description":"Keep It Simple & Stupid."},"scroll":{"progress_bar":{"enable":true},"percent":{"enable":true}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":true},"lazyload":{"enable":true},"version":"3.4.3"};
    KEEP.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 月前","year":"%s 年前"};
  </script>
<meta name="generator" content="Hexo 5.4.0"><link rel="stylesheet" href="/css/prism.css" type="text/css"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fas fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                Tadow
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                归档
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/categories"
                            >
                                分类
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/sbe"
                            >
                                订阅
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/links"
                            >
                                友链
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                关于
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">归档</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/categories">分类</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/sbe">订阅</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/links">友链</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">关于</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">Redis集群搭建</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="https://static.imlgw.top/blog/20210731/3C7hCSRR3lfq.png">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">Resolmi</span>
                        
                            <span class="author-label">BOSS</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;2018-05-27 00:00:00
    </span>
    
        <span class="article-categories article-meta-item">
            <i class="fas fa-folder"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/Web/">Web</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/%E6%B7%98%E6%B7%98%E5%95%86%E5%9F%8E/">淘淘商城</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/tags/Redis/">Redis</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/tags/%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/">集群搭建</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fas fa-file-word"></i>&nbsp;<span>2.4k 字</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fas fa-clock"></i>&nbsp;<span>11 分钟</span>
        </span>
    
    
        <span class="article-pv article-meta-item">
            <i class="fas fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <h1 id="Redis集群搭建"><a href="#Redis集群搭建" class="headerlink" title="Redis集群搭建"></a>Redis集群搭建</h1><h1 id="一、Redis简介"><a href="#一、Redis简介" class="headerlink" title="一、Redis简介"></a>一、Redis简介</h1><h3 id="1．关于关系型数据库和nosql数据库"><a href="#1．关于关系型数据库和nosql数据库" class="headerlink" title="1．关于关系型数据库和nosql数据库"></a>1．关于关系型数据库和nosql数据库</h3><p>   关系型数据库是基于关系表的数据库，最终会将数据持久化到<a href="">磁盘</a>上，而nosql数据  库是基于特殊的结构，并将数据存储到<a href="">内存</a>的数据库。从性能上而言，nosql数据库  要优于关系型数据库，从安全性上而言关系型数据库要优于nosql数据库，所以在实  际开发中一个项目中nosql和关系型数据库会一起使用，达到性能和安全性的双保证</p>
<h3 id="2-Redis的安装"><a href="#2-Redis的安装" class="headerlink" title="2 . Redis的安装"></a>2 . Redis的安装</h3><p>这里关于Redis的安装不想多说，实际生产中都是将Reids安装在Linux上的，这里主要是说集群的搭建。</p>
<h3 id="3-Redis的使用"><a href="#3-Redis的使用" class="headerlink" title="3. Redis的使用"></a>3. Redis的使用</h3><p>   我也是第一次接触Redis，使用其实也没什么说的 <a class="link"   target="_blank" rel="noopener" href="http://www.runoob.com/redis/redis-tutorial.html" >菜鸟教程<i class="fas fa-external-link-alt"></i></a>上都有，挺简单的。</p>
<h3 id="3-集群的搭建"><a href="#3-集群的搭建" class="headerlink" title="3. 集群的搭建"></a>3. 集群的搭建</h3><p>Redis要做集群必须要有至少三个节点否则他的投票机制无法运行<br>必须要有超过半数的节点都认为某一台Redis机器挂掉了才会认为集群挂掉了<br><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://p3.cdn.img9.top/ipfs/QmX5E7BViadysS6bZLs7Eeu1VSkvCHEEcUrkSVgWBjwNCx?3.png"
                      alt="Redis"
                ></p>
<p>这里再看看Redis集群的架构图<img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://p3.cdn.img9.top/ipfs/QmZ6PpYPozPHppvXGRBMY5LgnrsjAqDaT2s1NoUCT8EFuD?3.png"
                      alt="Redis"
                ><br>架构细节:</p>
<p>(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.</p>
<p>(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.</p>
<p>(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可</p>
<p>(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node&lt;-&gt;slot&lt;-&gt;value<br>Redis 集群中内置了 16384 个哈希槽，当需要在 Redis 集群中放置一个 key-value 时，redis 先对 key 使用 crc16 算法算出一个结果，然后把结果对 16384 求余数，这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽，redis 会根据节点数量大致均等的将哈希槽映射到不同的节点，所以Redis节点最多也就16384个节点最少3个节点。</p>
<ul>
<li>开始搭建<br>这里搭建的是有3个节点的最小的集群为保证集群的高可用每个节点都有一台备份机一共需要6台服务器，就需要开6个虚拟机这里其实做测试没那个必要，搭建一个伪分布式的就可以了，在一台虚拟机上搭建6个Redis实例</li>
<li>集群搭建环境<br>1、使用ruby脚本搭建集群。需要ruby的运行环境。<br>安装ruby<br>yum install ruby<br>yum install rubygems<br>(我虚拟机yum的时候一直报错无法解析，然后加了DNS后报404很奇怪 然后yum makecache后就好了)<br>2、安装ruby脚本运行使用的包<a class="link"   target="_blank" rel="noopener" href="https://pan.baidu.com/s/1bW6EUyeEevR5h1cT-g51mQ" >redis-3.0.0.gem<i class="fas fa-external-link-alt"></i></a>。<br>gem install redis-3.0.0.gem<br>这个脚本在你Redis源代码的src目录下redis-trib.rb这个脚本<br>3、创建6个Redis实例<br>直接copy5份编译后的Redis文件然后改下端口 把redis.conf(在Redis源代码下copy过来)里 cluster-enabled yes前注释去掉<br>3 、启动6个Redis这里可以写一个shell<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">cd redis01</span><br><span class="line">./redis-server redis.conf</span><br><span class="line">cd ..</span><br><span class="line">cd redis02</span><br><span class="line">./redis-server redis.conf</span><br><span class="line">cd ..</span><br><span class="line">cd redis03</span><br><span class="line">./redis-server redis.conf</span><br><span class="line">cd ..</span><br><span class="line">cd redis04</span><br><span class="line">./redis-server redis.conf</span><br><span class="line">cd ..</span><br><span class="line">cd redis05</span><br><span class="line">./redis-server redis.conf</span><br><span class="line">cd ..</span><br><span class="line">cd redis06</span><br><span class="line">./redis-server redis.conf</span><br><span class="line">cd ..</span><br></pre></td></tr></table></figure>
4、用Ruby搭建集群 只需要这条命令集群就搭建完毕了<br>./redis-trib.rb create –replicas 1 192.168.25.3:7001 192.168.25.3:7002 192.168.25.3:7003 192.168.25.3:7004 192.168.25.3:7005 192.168.25.3:7006<br>补 ：这里的ip不要写127.0.0.1 不然连接集群的时候就会报Too many Cluster redirections?错误 亲测。。。我不是写的127.0.0.1我是因为后来虚拟机ip变了然后<br>replicas后面参数1代表每个节点会有一台备份机所以后面的ip和端口号必须是偶数<h3 id="4-集群的使用"><a href="#4-集群的使用" class="headerlink" title="4. 集群的使用"></a>4. 集群的使用</h3>  redis01/redis-cli -p 7002 -c<br>后面的c代表连接的是集群 如果不加c就是单机的 如果存入数据的槽位不对应就会报错当然这是在命令行下的使用，windows下也有一些Redis的客户端。这里主要讲用Java代码连接Redis这就要用到Jedis了跟JDBC那一套差不多 这里直接讲在淘淘中实际是怎么用的吧<br>这里有两个版本集群版和单机版 平常测试就用单机版所以可以先写一个接口，抽取一些常用的方法</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> redis;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">JedisClient</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function">String <span class="title">set</span><span class="params">(String key, String value)</span></span>;</span><br><span class="line">	<span class="function">String <span class="title">get</span><span class="params">(String key)</span></span>;</span><br><span class="line">	<span class="function">Boolean <span class="title">exists</span><span class="params">(String key)</span></span>;</span><br><span class="line">	<span class="function">Long <span class="title">expire</span><span class="params">(String key, <span class="keyword">int</span> seconds)</span></span>;</span><br><span class="line">	<span class="function">Long <span class="title">ttl</span><span class="params">(String key)</span></span>;</span><br><span class="line">	<span class="function">Long <span class="title">incr</span><span class="params">(String key)</span></span>;</span><br><span class="line">	<span class="function">Long <span class="title">hset</span><span class="params">(String key, String field, String value)</span></span>;</span><br><span class="line">	<span class="function">String <span class="title">hget</span><span class="params">(String key, String field)</span></span>;</span><br><span class="line">	<span class="function">Long <span class="title">hdel</span><span class="params">(String key, String... field)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>然后写各自的实现类</p>
<ul>
<li>单机版</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> redis;</span><br><span class="line"><span class="keyword">import</span> lombok.Getter;</span><br><span class="line"><span class="keyword">import</span> lombok.Setter;</span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.JedisPool;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Getter</span><span class="meta">@Setter</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JedisClientPool</span> <span class="keyword">implements</span> <span class="title">JedisClient</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> JedisPool jedisPool;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">set</span><span class="params">(String key, String value)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		String result = jedis.set(key, value);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">get</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		String result = jedis.get(key);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Boolean <span class="title">exists</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		Boolean result = jedis.exists(key);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">expire</span><span class="params">(String key, <span class="keyword">int</span> seconds)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		Long result = jedis.expire(key, seconds);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">ttl</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		Long result = jedis.ttl(key);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">incr</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		Long result = jedis.incr(key);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">hset</span><span class="params">(String key, String field, String value)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		Long result = jedis.hset(key, field, value);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">hget</span><span class="params">(String key, String field)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		String result = jedis.hget(key, field);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">hdel</span><span class="params">(String key, String... field)</span> </span>&#123;</span><br><span class="line">		Jedis jedis = jedisPool.getResource();</span><br><span class="line">		Long result = jedis.hdel(key, field);</span><br><span class="line">		jedis.close();</span><br><span class="line">		<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li>集群版</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> redis;</span><br><span class="line"><span class="keyword">import</span> lombok.Getter;</span><br><span class="line"><span class="keyword">import</span> lombok.Setter;</span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.JedisCluster;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Getter</span><span class="meta">@Setter</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JedisClientCluster</span> <span class="keyword">implements</span> <span class="title">JedisClient</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> JedisCluster jedisCluster;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">set</span><span class="params">(String key, String value)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.set(key, value);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">get</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.get(key);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Boolean <span class="title">exists</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.exists(key);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">expire</span><span class="params">(String key, <span class="keyword">int</span> seconds)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.expire(key, seconds);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">ttl</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.ttl(key);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">incr</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.incr(key);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">hset</span><span class="params">(String key, String field, String value)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.hset(key, field, value);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">hget</span><span class="params">(String key, String field)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.hget(key, field);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Long <span class="title">hdel</span><span class="params">(String key, String... field)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> jedisCluster.hdel(key, field);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li>使用<br> 使用的时候就可以写个bean然后在需要的时候就可以直接注入了</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="line">&lt;beans xmlns=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="line">	xmlns:context=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span><br><span class="line">	xmlns:p=<span class="string">&quot;http://www.springframework.org/schema/p&quot;</span></span><br><span class="line">	xmlns:aop=<span class="string">&quot;http://www.springframework.org/schema/aop&quot;</span></span><br><span class="line">	xmlns:tx=<span class="string">&quot;http://www.springframework.org/schema/tx&quot;</span></span><br><span class="line">	xmlns:xsi=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="line">	xmlns:mybatis-spring=<span class="string">&quot;http://mybatis.org/schema/mybatis-spring&quot;</span></span><br><span class="line">	xsi:schemaLocation=<span class="string">&quot;http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd</span></span><br><span class="line"><span class="string">		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd</span></span><br><span class="line"><span class="string">		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd</span></span><br><span class="line"><span class="string">		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd</span></span><br><span class="line"><span class="string">		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd</span></span><br><span class="line"><span class="string">		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd&quot;</span>&gt;</span><br><span class="line">	&lt;!--单机版--&gt;</span><br><span class="line">    &lt;!-- 	&lt;bean id=<span class="string">&quot;jedisClientPool&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.JedisClientPool&quot;</span>&gt;</span><br><span class="line">		&lt;property name=<span class="string">&quot;jedisPool&quot;</span> ref=<span class="string">&quot;jedisPool&quot;</span> /&gt;</span><br><span class="line">	&lt;/bean&gt;</span><br><span class="line">	&lt;bean id=<span class="string">&quot;jedisPool&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.clients.jedis.JedisPool&quot;</span>&gt;</span><br><span class="line">		&lt;constructor-arg name=<span class="string">&quot;host&quot;</span> value=<span class="string">&quot;192.168.25.4&quot;</span>/&gt;</span><br><span class="line">		&lt;constructor-arg name=<span class="string">&quot;port&quot;</span> value=<span class="string">&quot;6379&quot;</span>/&gt;</span><br><span class="line">	&lt;/bean&gt; --&gt;</span><br><span class="line">	&lt;!--集群版--&gt;</span><br><span class="line">	&lt;bean id=<span class="string">&quot;jedisClientCluster&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.JedisClientCluster&quot;</span>&gt;</span><br><span class="line">		&lt;property name=<span class="string">&quot;jedisCluster&quot;</span> ref=<span class="string">&quot;jedisCluster&quot;</span>/&gt;		</span><br><span class="line">	&lt;/bean&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;bean id=<span class="string">&quot;jedisCluster&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.clients.jedis.JedisCluster&quot;</span>&gt;</span><br><span class="line">		&lt;constructor-arg name=<span class="string">&quot;nodes&quot;</span>&gt;</span><br><span class="line">			&lt;set&gt;</span><br><span class="line">				&lt;bean <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.clients.jedis.HostAndPort&quot;</span>&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;host&quot;</span> value=<span class="string">&quot;192.168.25.4&quot;</span>/&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;port&quot;</span> value=<span class="string">&quot;7001&quot;</span>/&gt;</span><br><span class="line">				&lt;/bean&gt;</span><br><span class="line">				&lt;bean <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.clients.jedis.HostAndPort&quot;</span>&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;host&quot;</span> value=<span class="string">&quot;192.168.25.4&quot;</span>/&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;port&quot;</span> value=<span class="string">&quot;7002&quot;</span>/&gt;</span><br><span class="line">				&lt;/bean&gt;</span><br><span class="line">				&lt;bean <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.clients.jedis.HostAndPort&quot;</span>&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;host&quot;</span> value=<span class="string">&quot;192.168.25.4&quot;</span>/&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;port&quot;</span> value=<span class="string">&quot;7003&quot;</span>/&gt;</span><br><span class="line">				&lt;/bean&gt;</span><br><span class="line">				&lt;bean <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.clients.jedis.HostAndPort&quot;</span>&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;host&quot;</span> value=<span class="string">&quot;192.168.25.4&quot;</span>/&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;port&quot;</span> value=<span class="string">&quot;7004&quot;</span>/&gt;</span><br><span class="line">				&lt;/bean&gt;</span><br><span class="line">				&lt;bean <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.clients.jedis.HostAndPort&quot;</span>&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;host&quot;</span> value=<span class="string">&quot;192.168.25.4&quot;</span>/&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;port&quot;</span> value=<span class="string">&quot;7005&quot;</span>/&gt;</span><br><span class="line">				&lt;/bean&gt;</span><br><span class="line">				&lt;bean <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;redis.clients.jedis.HostAndPort&quot;</span>&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;host&quot;</span> value=<span class="string">&quot;192.168.25.4&quot;</span>/&gt;</span><br><span class="line">					&lt;constructor-arg name=<span class="string">&quot;port&quot;</span> value=<span class="string">&quot;7006&quot;</span>/&gt;</span><br><span class="line">				&lt;/bean&gt;</span><br><span class="line">			&lt;/set&gt;</span><br><span class="line">		&lt;/constructor-arg&gt;			</span><br><span class="line">	&lt;/bean&gt;</span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure>
<p>这里在集群的时候遇到了问题就是我上面提到的ip的问题不要写127.0.0.1要写实际的内网ip以后如果ip变了就只能重新来一次了，那个命令我试了下执行第二次，报错说我那个节点不为空，<strong>也许</strong>把数据清空了就可以了。</p>
<ul>
<li>测试<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testRedis</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="comment">//初始化Spring容器</span></span><br><span class="line">	ApplicationContext applicationContext = <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;classpath:spring/applicationContext-redis.xml&quot;</span>);</span><br><span class="line">	<span class="comment">//从容器中获取jedisClient对象 (单机版) </span></span><br><span class="line">	JedisClient jedisClient=applicationContext.getBean(JedisClient.class);</span><br><span class="line">	jedisClient.set(<span class="string">&quot;testRedis&quot;</span>, <span class="string">&quot;Hello World&quot;</span>);</span><br><span class="line">	System.out.println(jedisClient.get(<span class="string">&quot;testRedis&quot;</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>

        </div>

        
            <div class="post-copyright-info">
                <div class="article-copyright-info-container">
    <ul>
        <li>本文标题：Redis集群搭建</li>
        <li>本文作者：Resolmi</li>
        <li>创建时间：2018-05-27 00:00:00</li>
        <li>
            本文链接：https://imlgw.top/2018/05/27/3e4345d9/
        </li>
        <li>
            版权声明：本博客所有文章除特别声明外，均采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">BY-NC-SA</a> 许可协议。转载请注明出处！
        </li>
    </ul>
</div>

            </div>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/2018/06/17/d5f81032/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">Nmap学习</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2018/05/23/d2be9bab/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">Mybatis逆向工程对text的特殊处理</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
            <div class="comment-container">
                <div class="comments-container">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fas fa-comments">&nbsp;评论</i>
    </div>
    

        
            <section class="disqus-comments">
<div id="disqus_thread">
  <noscript>Please enable JavaScript to view the <a target="_blank" rel="noopener" href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</section>

<script>
var disqus_shortname = 'imlgw';

var disqus_url = 'https://imlgw.top/2018/05/27/3e4345d9/';

(function(){
  var dsq = document.createElement('script');
  dsq.type = 'text/javascript';
  dsq.async = true;
  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>

<script id="dsq-count-scr" src="//imlgw.disqus.com/count.js" async></script>
        
    
</div>

            </div>
        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2018</span>&nbsp;-&nbsp;
            
            2021&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">Resolmi</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv"></span>&ensp;
                    </span>
                
                
                    <span id="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        
            <div class="icp-info info-item"><a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn">鄂ICP备18011208号</a></div>
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fas fa-comment"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Redis%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA"><span class="nav-number">1.</span> <span class="nav-text">Redis集群搭建</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81Redis%E7%AE%80%E4%BB%8B"><span class="nav-number">2.</span> <span class="nav-text">一、Redis简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1%EF%BC%8E%E5%85%B3%E4%BA%8E%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93%E5%92%8Cnosql%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">2.0.1.</span> <span class="nav-text">1．关于关系型数据库和nosql数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-Redis%E7%9A%84%E5%AE%89%E8%A3%85"><span class="nav-number">2.0.2.</span> <span class="nav-text">2 . Redis的安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-Redis%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-number">2.0.3.</span> <span class="nav-text">3. Redis的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E9%9B%86%E7%BE%A4%E7%9A%84%E6%90%AD%E5%BB%BA"><span class="nav-number">2.0.4.</span> <span class="nav-text">3. 集群的搭建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E9%9B%86%E7%BE%A4%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-number">2.0.5.</span> <span class="nav-text">4. 集群的使用</span></a></li></ol></li></ol></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>



<script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/utils.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/main.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/header-shrink.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/back2top.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/dark-light-toggle.js"></script>


    <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/local-search.js"></script>



    <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/code-copy.js"></script>



    <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/lazyload.js"></script>


<div class="post-scripts pjax">
    
        <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/left-side-toggle.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/libs/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/toc.js"></script>
    
</div>


    <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/libs/pjax.min.js"></script>
<script>
    window.addEventListener('DOMContentLoaded', () => {
        window.pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax'
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            KEEP.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            KEEP.utils.pjaxProgressBarEnd();
            window.pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            KEEP.refresh();
        });
    });
</script>



<script src="https://cdn.jsdelivr.net/npm/live2d-widget@3.x/lib/L2Dwidget.min.js"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"https://cdn.jsdelivr.net/npm/live2d-widget-model-hijiki@1.0.5/assets/hijiki.model.json"},"display":{"superSample":2,"width":160,"height":320,"position":"right","hOffset":0,"vOffset":-70},"mobile":{"show":false,"scale":0.2},"log":false});</script></body>
</html>
